package com.RMP
{
    import com.gskinner.text.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import com.greensock.*;

    public class ProjDescShell extends MovieClip
    {
        public var myXMLList:XML;
        var isBuilding:Boolean;
        public var imageBtn:Sprite;
        public var loader:Loader;
        var counter:int;
        var myURL:String;
        var fadeSpeed:Number;
        public var showBtn:Boolean = true;
        var nav:ProjPaginatedMenu;
        public var fadeInBitmap:Boolean = true;
        public var image:Bitmap;
        public var coverSprite:Object;
        var frame:Frame;
        var fieldArray:Array;
        public var textFormat:Object;
        var tf:TextFlow;
        var myRoot:MovieClip;
        var currNavId:int;
        public var hasSub:Boolean;
        var bigField:BigField;
        var styler:StyleSheet;
        var myText:String;
        var myBorder:Boolean;

        public function ProjDescShell(param1:MovieClip, param2:Number, param3:String, param4:String, param5:Boolean, param6 = null) : void
        {
            fadeInBitmap = true;
            showBtn = true;
            myRoot = param1;
            fadeSpeed = param2;
            myText = param3;
            myURL = param4;
            myBorder = param5;
            if (param6 == null)
            {
                init();
            }
            else
            {
                initWithBitmap(param6);
            }
            return;
        }

        public function incomingBitmap(param1) : void
        {
            image = Bitmap(param1);
            doResize();
            image.alpha = 1;
            addChild(image);
            if (myBorder == true)
            {
                frame = new Frame();
                frame.alpha = 1;
                addChild(frame);
                doResize();
            }
            image.smoothing = true;
            imageBtn = new Sprite();
            imageBtn.graphics.beginFill(16777215, 1);
            imageBtn.graphics.drawRect(0, 0, image.width, image.height);
            if (showBtn == true)
            {
                imageBtn.buttonMode = true;
                imageBtn.addEventListener(MouseEvent.CLICK, imageBtnClick, false, 0, true);
                imageBtn.addEventListener(MouseEvent.CLICK, imageBtnOver, false, 0, true);
                imageBtn.addEventListener(MouseEvent.CLICK, imageBtnOut, false, 0, true);
            }
            else
            {
                imageBtn.visible = false;
            }
            imageBtn.alpha = 0;
            addChild(imageBtn);
            doResize();
            return;
        }

        function buildCoverSprite() : void
        {
            coverSprite = new Sprite();
            coverSprite.graphics.beginFill(16777215, 1);
            coverSprite.graphics.drawRect(0, 0, 1, 1);
            coverSprite.graphics.endFill();
            coverSprite.buttonMode = true;
            coverSprite.useHandCursor = false;
            coverSprite.alpha = 0;
            coverSprite.visible = false;
            addChild(coverSprite);
            return;
        }

        function init() : void
        {
            buildStyles();
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
            if (myURL != "")
            {
                loader.load(new URLRequest(myRoot.path + myURL));
            }
            counter = 0;
            currNavId = 0;
            buildField();
            nav = new ProjPaginatedMenu(myRoot, fadeSpeed, counter);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            addChild(nav);
            buildCoverSprite();
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            updateGrid();
            if (hasSub == true)
            {
                nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            }
            else
            {
                nav.x = Math.round(9 * (myRoot.unitW + myRoot.gutterW));
            }
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            return;
        }

        function imageBtnClick(event:MouseEvent) : void
        {
            dispatchEvent(new ImageBtnClickEvent());
            return;
        }

        function buildField() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:* = undefined;
            textFormat = new TextFormat();
            textFormat.font = new HelveticaLightEmbed().fontName;
            textFormat.size = 14;
            textFormat.letterSpacing = 0;
            textFormat.leading = -4;
            textFormat.leftMargin = 0;
            textFormat.rightMargin = 0;
            textFormat.color = myRoot.coldColor;
            fieldArray = new Array();
            isBuilding = true;
            bigField = new BigField();
            bigField.name = "field" + 0;
            bigField.field.antiAliasType = AntiAliasType.ADVANCED;
            bigField.field.gridFitType = GridFitType.PIXEL;
            bigField.field.selectable = false;
            bigField.field.embedFonts = true;
            bigField.field.multiline = true;
            bigField.field.wordWrap = true;
            bigField.y = -1;
            bigField.field.htmlText = myText;
            bigField.field.defaultTextFormat = textFormat;
            addChild(bigField);
            fieldArray.push(bigField.field);
            tf = new TextFlow(fieldArray);
            while (isBuilding == true)
            {
                
                bigField = new BigField();
                bigField.name = "field" + fieldArray.length;
                bigField.field.antiAliasType = AntiAliasType.ADVANCED;
                bigField.field.gridFitType = GridFitType.PIXEL;
                bigField.field.selectable = false;
                bigField.field.mouseWheelEnabled = false;
                bigField.field.embedFonts = true;
                bigField.field.multiline = true;
                bigField.field.wordWrap = true;
                bigField.field.defaultTextFormat = textFormat;
                bigField.y = -1;
                bigField.visible = false;
                fieldArray.push(bigField.field);
                tf.textFields = fieldArray;
                tf.reflow();
                if (bigField.field.text == "")
                {
                    isBuilding = false;
                    fieldArray.pop();
                    bigField = null;
                    continue;
                }
                addChild(bigField);
            }
            counter = Math.ceil(fieldArray.length / 2);
            currNavId = 0;
            _loc_1 = 0;
            while (_loc_1 < fieldArray.length)
            {
                
                fieldArray[_loc_1].styleSheet = styler;
                fieldArray[_loc_1].htmlText = "<p>" + myText + "</p>";
                if (_loc_1 > 0)
                {
                    if (fieldArray[(_loc_1 - 1)].maxScrollV > 1)
                    {
                        _loc_3 = 0;
                        while (_loc_3 < fieldArray[(_loc_1 - 1)].bottomScrollV)
                        {
                            
                            fieldArray[_loc_1].htmlText = fieldArray[_loc_1].htmlText + "<br>";
                            fieldArray[_loc_1].scrollV = fieldArray[(_loc_1 - 1)].bottomScrollV + 1;
                            _loc_3 = _loc_3 + 1;
                        }
                    }
                }
                _loc_1++;
            }
            _loc_2 = 0;
            while (_loc_2 < fieldArray.length)
            {
                
                fieldArray[_loc_2].selectable = false;
                fieldArray[_loc_2].mouseWheelEnabled = false;
                _loc_2++;
            }
            return;
        }

        function buildStyles() : void
        {
            var _loc_1:* = undefined;
            var _loc_2:* = undefined;
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            var _loc_7:Object = null;
            var _loc_8:* = undefined;
            var _loc_9:Object = null;
            _loc_1 = new HelveticaLightEmbed().fontName;
            _loc_2 = new HelveticaBoldEmbed().fontName;
            styler = new StyleSheet();
            _loc_3 = new Object();
            _loc_3.color = myRoot.coldColorHTML;
            _loc_3.fontFamily = _loc_1;
            _loc_3.leading = -4;
            _loc_4 = new Object();
            _loc_4.color = myRoot.coldColorHTML;
            _loc_4.textDecoration = "none";
            _loc_5 = new Object();
            _loc_5.color = myRoot.hotColorHTML;
            _loc_5.textDecoration = "none";
            _loc_6 = new Object();
            _loc_6.color = myRoot.coldColorHTML;
            _loc_6.fontFamily = _loc_2;
            _loc_7 = new Object();
            _loc_7.color = myRoot.hotColorHTML;
            _loc_7.fontFamily = _loc_2;
            _loc_8 = new HelveticaItalicEmbed().fontName;
            _loc_9 = new Object();
            _loc_9.color = myRoot.coldColorHTML;
            _loc_9.fontFamily = _loc_8;
            styler.setStyle(".myItalic", _loc_9);
            styler.setStyle("p", _loc_3);
            styler.setStyle("a:link", _loc_4);
            styler.setStyle("a:hover", _loc_5);
            styler.setStyle(".mybold", _loc_6);
            return;
        }

        public function doResize() : void
        {
            var _loc_1:int = 0;
            var _loc_2:* = undefined;
            if (coverSprite != null)
            {
                coverSprite.x = Math.round(4 * (myRoot.unitW + myRoot.gutterW));
                coverSprite.y = Math.round(-2 + myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH));
                coverSprite.width = 8 * (myRoot.unitW + myRoot.gutterW);
                coverSprite.height = 4 * (myRoot.unitH + myRoot.gutterH);
            }
            if (hasSub == true)
            {
                nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            }
            else
            {
                nav.x = Math.round(9 * (myRoot.unitW + myRoot.gutterW));
            }
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            if (image != null)
            {
                image.y = Math.round(myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH));
                var _loc_3:* = myRoot.unitW / 75;
                image.scaleY = myRoot.unitW / 75;
                image.scaleX = _loc_3;
                if (imageBtn != null)
                {
                    imageBtn.y = Math.round(myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH));
                    imageBtn.width = image.width;
                    imageBtn.height = image.height;
                }
            }
            _loc_1 = 0;
            while (_loc_1 < fieldArray.length)
            {
                
                var _loc_3:* = myRoot.unitW / 75;
                fieldArray[_loc_1].scaleY = myRoot.unitW / 75;
                fieldArray[_loc_1].scaleX = _loc_3;
                _loc_2 = _loc_1 % 2;
                fieldArray[_loc_1].parent.x = Math.round((4 + 4 * _loc_2) * (myRoot.unitW + myRoot.gutterW));
                fieldArray[_loc_1].parent.y = Math.round(-5 * myRoot.unitW / 75 + myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH));
                _loc_1++;
            }
            if (frame != null)
            {
                frame.y = image.y;
                frame.resizeFrame(image.width, image.height);
            }
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function menuClick(param1:PaginatedMenuClick)
        {
            currNavId = param1.currentTarget.currNavId;
            updateGrid();
            return;
        }

        function imageBtnOver(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {alpha:0.5, overwrite:true});
            return;
        }

        public function reloadImage() : void
        {
            if (image == null)
            {
                loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
                if (myURL != "")
                {
                    loader.load(new URLRequest(myRoot.path + myURL));
                }
            }
            else
            {
                image.visible = true;
                image.alpha = 1;
                if (myBorder == true)
                {
                    frame.visible = true;
                    frame.alpha = 1;
                }
                addChild(image);
            }
            return;
        }

        function imageBtnOut(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {alpha:0, overwrite:true});
            return;
        }

        function onLoadComplete(event:Event) : void
        {
            var event:* = event;
            image = Bitmap(loader.content);
            doResize();
            image.alpha = 0;
            addChild(image);
            if (myBorder == true)
            {
                frame = new Frame();
                frame.alpha = 0;
                addChild(frame);
                doResize();
                if (fadeInBitmap == true)
                {
                    TweenMax.to(frame, fadeSpeed, {alpha:1, overwrite:true});
                }
                else
                {
                    frame.alpha = 1;
                }
            }
            if (fadeInBitmap == true)
            {
                TweenMax.to(image, fadeSpeed, {alpha:1, overwrite:true, onComplete:function ()
            {
                image.smoothing = true;
                return;
            }
            });
            }
            else
            {
                image.alpha = 1;
                image.smoothing = true;
            }
            fadeInBitmap = true;
            imageBtn = new Sprite();
            imageBtn.graphics.beginFill(16777215, 1);
            imageBtn.graphics.drawRect(0, 0, image.width, image.height);
            if (showBtn == true)
            {
                imageBtn.buttonMode = true;
                imageBtn.addEventListener(MouseEvent.CLICK, imageBtnClick, false, 0, true);
                imageBtn.addEventListener(MouseEvent.MOUSE_OVER, imageBtnOver, false, 0, true);
                imageBtn.addEventListener(MouseEvent.MOUSE_OUT, imageBtnOut, false, 0, true);
                imageBtn.alpha = 0;
            }
            else
            {
                imageBtn.visible = false;
            }
            addChild(imageBtn);
            doResize();
            return;
        }

        function initWithBitmap(param1) : void
        {
            buildStyles();
            counter = 0;
            currNavId = 0;
            buildField();
            nav = new ProjPaginatedMenu(myRoot, fadeSpeed, counter);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            addChild(nav);
            buildCoverSprite();
            doResize();
            updateGrid();
            if (hasSub == true)
            {
                nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            }
            else
            {
                nav.x = Math.round(9 * (myRoot.unitW + myRoot.gutterW));
            }
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            image = new Bitmap(param1.bitmapData);
            image.alpha = 1;
            addChild(image);
            if (myBorder == true)
            {
                frame = new Frame();
                frame.alpha = 0;
                addChild(frame);
                doResize();
                frame.alpha = 1;
            }
            fadeInBitmap = true;
            imageBtn = new Sprite();
            imageBtn.graphics.beginFill(16777215, 1);
            imageBtn.graphics.drawRect(0, 0, image.width, image.height);
            if (showBtn == true)
            {
                imageBtn.buttonMode = true;
                imageBtn.addEventListener(MouseEvent.CLICK, imageBtnClick, false, 0, true);
                imageBtn.addEventListener(MouseEvent.CLICK, imageBtnOver, false, 0, true);
                imageBtn.addEventListener(MouseEvent.CLICK, imageBtnOut, false, 0, true);
            }
            else
            {
                imageBtn.visible = false;
            }
            imageBtn.alpha = 0;
            addChild(imageBtn);
            doResize();
            return;
        }

        function updateGrid() : void
        {
            var i:int;
            var foo:*;
            i;
            while (i < fieldArray.length)
            {
                
                if (i == 2 * currNavId || i == 2 * currNavId + 1)
                {
                    fieldArray[i].parent.visible = true;
                    TweenMax.to(fieldArray[i].parent, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true});
                }
                else
                {
                    foo = fieldArray[i].parent;
                    TweenMax.to(foo, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                foo.visible = false;
                return;
            }
            });
                }
                i = (i + 1);
            }
            doResize();
            return;
        }

        public function fadeOutCoverSprite() : void
        {
            coverSprite.visible = true;
            coverSprite.alpha = 1;
            visible = false;
            TweenMax.to(this, 0, {alpha:1, delay:fadeSpeed - fadeSpeed / 10, overwrite:true, onComplete:function ()
            {
                visible = true;
                return;
            }
            });
            TweenMax.to(coverSprite, fadeSpeed, {alpha:0, delay:fadeSpeed, overwrite:true, onComplete:function ()
            {
                coverSprite.visible = false;
                return;
            }
            });
            return;
        }

        public function fadeInCoverSprite() : void
        {
            myRoot.menuShell.hRule3On();
            coverSprite.alpha = 0;
            coverSprite.visible = true;
            TweenMax.to(coverSprite, fadeSpeed, {alpha:1, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        public function fadeInShell() : void
        {
            myRoot.menuShell.hRule3On();
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true});
            return;
        }

    }
}
